(function ($) {
$.fn.webForm = function (options) {
// This is the easiest way to have default options.
var settings = $.extend({}, $.fn.webForm.defaults, options);
var element = this;
var elementId = element.attr('id');
//create the form
this.append("
");
$("#" + elementId + " #webToLeadForm").append("
");
$("#" + elementId + " #webToLeadFormInner").append("
");
$("#" + elementId + " #webToLeadFormInner").append("
");
//now add form components
$("#" + elementId + " #webToLeadFormCol1").append("" +
"Name " +
" " +
"
");
$("#" + elementId + " #webToLeadFormCol1").append("" +
"First Name " +
" " +
"
");
$("#" + elementId + " #webToLeadFormCol1").append("" +
"Last Name " +
" " +
"
");
$("#" + elementId + " #webToLeadFormCol1").append("" +
"Email " +
" " +
"
");
$("#" + elementId + " #webToLeadFormCol1").append("" +
"Telephone " +
" " +
"
");
$("#" + elementId + " #webToLeadFormCol1").append("" +
"Postcode " +
" " +
"
");
$("#" + elementId + " #webToLeadFormCol1").append("" +
"Address Line 1 " +
" " +
"
");
$("#" + elementId + " #webToLeadFormCol1").append("" +
"Preferred date of contact " +
" " +
"
");
$("#" + elementId + " #webToLeadFormCol1").append("" +
"Preferred time of contact " +
" " +
"" + (settings.showFieldLabels ? "" : "Preferred time of contact") + " " +
"09:00 - 10:00 " +
"10:00 - 11:00 " +
"11:00 - 12:00 " +
"12:00 - 13:00 " +
"13:00 - 14:00 " +
"14:00 - 15:00 " +
"15:00 - 16:00 " +
"16:00 - 17:00 " +
"17:00 - 18:00 " +
" " +
"
");
$("#" + elementId + " #webToLeadFormCol1").append("" +
"Which branch is your closest? " +
"" + (settings.showFieldLabels ? "" : "Which branch is your closest? *") + " " +
"
");
//make api call to retrive list of branches
$.ajax({
url: settings.crmUrl + "/WebForm/GetAllCompanyBranches/",
type: "GET",
data: { APIKey: settings.apiKey },
contentType: "application/json; charset=utf-8",
success: function (result) {
var item = result;
$.each(result, function (i, val) {
$("#" + elementId + " #webToLeadFormCol1 #companyBranch").append("" + val.Name + " ");
});
},
error: function (result) {
console.log("Error getting company branches");
}
});
$("#" + elementId + " #webToLeadFormCol1").append("" +
"Product of Interest " +
"" + (settings.showFieldLabels ? "" : "Product of Interest *") + " " +
"
");
//make api call to retrive list of product interests
$.ajax({
url: settings.crmUrl + "/WebForm/GetAllProductInterests/",
type: "GET",
data: { APIKey: settings.apiKey },
contentType: "application/json; charset=utf-8",
success: function (result) {
var item = result;
$.each(result, function (i, val) {
$("#" + elementId + " #webToLeadFormCol1 #product").append("" + val.Name + " ");
});
},
error: function (result) {
console.log("Error getting product interests");
}
});
$("#" + elementId + " #webToLeadFormCol1").append("" +
"Project Type " +
"" +
"" + (settings.showFieldLabels ? "" : "Type of Project *") + " " +
" " +
"
");
//make api call to retrive list of lead sources
$.ajax({
url: settings.crmUrl + "/WebForm/GetAllProjectTypes/",
type: "GET",
data: { APIKey: settings.apiKey },
contentType: "application/json; charset=utf-8",
success: function (result) {
var item = result;
$.each(result, function (i, val) {
$("#" + elementId + " #webToLeadFormCol1 #projecttype").append("" + val.Name + " ");
});
},
error: function (result) {
console.log("Error getting project types");
}
});
$("#" + elementId + " #webToLeadFormCol1").append("" +
"How did you hear about us? " +
"" + (settings.showFieldLabels ? "" : "How did you hear about us? *") + " " +
"
");
//make api call to retrive list of lead sources
$.ajax({
url: settings.crmUrl + "/WebForm/GetAllLeadSources/",
type: "GET",
data: { APIKey: settings.apiKey },
contentType: "application/json; charset=utf-8",
success: function (result) {
var item = result;
$.each(result, function (i, val) {
$("#" + elementId + " #webToLeadFormCol1 #leadSource").append("" + val.Name + " ");
});
},
error: function (result) {
console.log("Error getting lead sources");
}
});
$("#" + elementId + " #webToLeadFormCol2").append("" +
"Your message " +
" " +
" " +
"
");
$("#" + elementId + " #webToLeadFormCol2").append("" +
"
If you would like to upload your plans or an image (max. file size 5MB) please click below and browse to your document.
" +
"
" +
"
");
$("#" + elementId + " #webToLeadFormCol2").append("" +
"
" + settings.privacyNotice + "
" +
"
");
$("#" + elementId + " #webToLeadFormCol2").append("" +
"
" + settings.marketingPreferencesNotice + "
" +
"
",
"
");
$.ajax({
url: settings.crmUrl + "/WebForm/GetAllMarketingTypes/",
type: "GET",
data: { APIKey: settings.apiKey },
contentType: "application/json; charset=utf-8",
success: function (result) {
var item = result;
$.each(result, function (i, val) {
$("#" + elementId + " #marketingPrefTypes").append(" " + val.Name + "
");
});
},
error: function (result) {
console.log("Error getting marketing types");
}
});
$("#" + elementId + " #webToLeadFormCol2").append("" +
" " +
"
");
$("#" + elementId + ' #submitButton').on("click", submitForm);
function submitForm(e) {
e.preventDefault();
$("#" + elementId + " #webToLeadForm").validate();
if ($("#" + elementId + " #webToLeadForm").valid()) {
$("#" + elementId + ' #submitButton').attr("disabled", "disabled");
$("#" + elementId + ' #submitButton').val('Please wait..');
var data = new FormData();
var filesElement = $("#" + elementId + ' #fileUpload');
var files = filesElement[0].files;
if (files.length > 0) {
if (window.FormData !== undefined) {
for (var x = 0; x < files.length; x++) {
data.append("file" + x, files[x]);
}
}
}
data.append("APIKey", settings.apiKey);
data.append("formSource", settings.formSourceName);
var name = $("#" + elementId + ' #webToLeadForm #name').val();
data.append("name", name);
var firstName = $("#" + elementId + ' #webToLeadForm #firstName').val();
data.append("firstName", firstName);
var lastName = $("#" + elementId + ' #webToLeadForm #lastName').val();
data.append("lastName", lastName);
var email = $("#" + elementId + ' #webToLeadForm #email').val();
data.append("email", email);
var telephone = $("#" + elementId + ' #webToLeadForm #telephone').val();
data.append("telephone", telephone);
var postcode = $("#" + elementId + ' #webToLeadForm #postcode').val();
data.append("postcode", postcode);
var addressLine1 = $("#" + elementId + ' #webToLeadForm #addressLine1').val();
data.append("addressLine1", addressLine1);
var prefdate = $("#" + elementId + ' #webToLeadForm #prefdate').val();
data.append("prefdate", prefdate);
var preftime = $("#" + elementId + ' #webToLeadForm #preftime option:selected').val();
data.append("preftime", preftime);
var productInterestId = $("#" + elementId + ' #webToLeadForm #product option:selected').val();
data.append("productInterestId", productInterestId);
var projectTypeId = $("#" + elementId + ' #webToLeadForm #projecttype option:selected').val();
data.append("projectTypeId", projectTypeId);
var leadSourceId = $("#" + elementId + ' #webToLeadForm #leadSource option:selected').val();
data.append("leadSourceId", leadSourceId);
var companyBranchId = $("#" + elementId + ' #webToLeadForm #companyBranch option:selected').val();
data.append("companyBranchId", companyBranchId);
var message = $("#" + elementId + ' #webToLeadForm #message').val();
var extraMessage = settings.getAdditionalMessageTextHandler.call(this);
if (extraMessage.length) {
message += " " + extraMessage;
}
data.append("message", message);
$("#" + elementId + ' #marketingPrefTypes input[type="checkbox"]:checked').each(function (index, element) {
data.append("selectedMarketing[" + index + "]", $(element).val());
});
$.ajax({
url: settings.crmUrl + "/WebForm/ReceiveLead/",
type: "POST",
processData: false,
data: data,
contentType: false,
success: function (result) {
if (result.success) {
settings.submitHandler.call(this);
}
else {
console.log("Error saving enquiry: " + result.message);
}
$("#" + elementId + ' #submitButton').removeAttr("disabled");
$("#" + elementId + ' #submitButton').val(settings.submitButtonText);
},
error: function (result) {
alert("Error saving enquiry");
$("#" + elementId + ' #submitButton').removeAttr("disabled");
$("#" + elementId + ' #submitButton').val(settings.submitButtonText);
}
});
}
}
$("#" + elementId + " #webToLeadForm").validate();
return this;
};
// Plugin defaults – added as a property on our plugin function.
$.fn.webForm.defaults = {
apiKey: "",
crmUrl: "",
formSourceName: "Contact",
formClass: "webToLeadForm",
formInnerClass: "row",
column1Class: "col-sm-4",
column2Class: "col-sm-8",
fieldWrapperClass: "form-group",
inputClass: "form-control",
labelClass: "control-label",
messageRows: "5",
showNameField: true,
showLastNameField: false,
showFirstNameField: false,
showEmailField: true,
showTelephoneField: true,
showPostcodeField: true,
showAppointmentDate: true,
showAppointmentTime: true,
showProductInterestField: true,
showProjectTypeField: true,
showLeadSourceField: true,
showCompanyBranchField: false,
showMessageField: true,
showFileUploadField: true,
showFieldLabels: true,
privacyNotice: "This form collects information provided by you so that we may respond to your enquiry. See our Privacy Policy for information about how we collect and store your data.",
marketingPreferencesNotice: "I give permission to be contacted using the following mediums:",
submitButtonClasses: "btn btn-primary request-quote",
submitButtonText: "Submit",
submitHandler: function () { },
getAdditionalMessageTextHandler: function () { return ""; }
};
}(jQuery));